package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.VipUserListBean;
import cn.wikiflyer.beikang.bean.VipUserListBean.VipUserBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.view.WKHeader;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class UserVipListAct extends BaseActivity implements WKListViewInterface {

	@ViewInject(id = R.id.header)
	private WKHeader header;
	private TextView tv_VipSum;

	private WKListViewUtils<VipUserBean, VipUserListBean> listUtils;

	private EditText input;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_vip_list_layout);
	}

	@Override
	public void init() {
		initList();
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	private void initList() {
		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);

		View view_header = View.inflate(ctx, R.layout.header_user_vip_list,
				null);
		tv_VipSum = (TextView) view_header.findViewById(R.id.viplistheader_sum);
		view_header.findViewById(R.id.btn_invite).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(ctx, UserShareAct.class);
						startActivity(intent);
					}
				});
		input = (EditText) view_header.findViewById(R.id.viplistheader_edit);
		listUtils = new WKListViewUtils<VipUserBean, VipUserListBean>() {
		};
		listUtils.init(ctx, this, pList, view_header);

		input.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
				update("" + s.toString());
			}
		});
	}

	private void update(String key) {
		listUtils.list.clear();
		try {
			if (key == null || key.equals("")) {
				listUtils.list.addAll(listUtils.bean.data.list);
			} else {
				for (int i = 0; i < listUtils.bean.data.list.size(); i++) {
					VipUserBean b = listUtils.bean.data.list.get(i);
					if (b.search(key)) {
						listUtils.list.add(b);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			listUtils.list.addAll(listUtils.bean.data.list);
		}
		listUtils.adapter.notifyDataSetChanged();
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getVipMember(listUtils.page, listUtils.pageCount,
				isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<VipUserListBean>>() {
		}.getType();
		Gson gson = new Gson();
		BKBaseBean<VipUserListBean> base = gson.fromJson(result, cvbType);
		if (base.status == 1) {
			tv_VipSum.setText("" + base.data.list.size());
			listUtils.bean = base;
			// 使用数据
			listUtils.list.addAll(listUtils.bean.data.list);
			listUtils.adapter.notifyDataSetChanged();
		}
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.act_user_vip_list_item;
	}

	class MyViewHolder extends WKViewHolder<VipUserBean> {
		TextView vip_number;
		TextView vip_name;
		TextView vip_level;
		TextView vip_count;
		TextView vip_tip;
		View itemView;
		View clickView;

		public MyViewHolder(View view) {
			itemView = view;
			clickView = view.findViewById(R.id.click_view);

			vip_number = (TextView) view.findViewById(R.id.vipitem_number);
			vip_name = (TextView) view.findViewById(R.id.vipitem_name);
			vip_level = (TextView) view.findViewById(R.id.vipitem_level);
			vip_count = (TextView) view.findViewById(R.id.vipitem_count);
			vip_tip = (TextView) view.findViewById(R.id.vipitem_tip);
		}

		@Override
		public void setData(VipUserBean t) {
			vip_number.setText(t.memberno);
			vip_name.setText(t.username);
			vip_count.setText(t.sales_volume + "");
			vip_level.setText(t.level_title);
			if (!t.upgrade.equals("false")) {
				vip_tip.setVisibility(View.VISIBLE);
				vip_tip.setText(t.upgrade);
			} else {
				vip_tip.setVisibility(View.INVISIBLE);
			}
			if (app().showMyMember) {
				clickView.setTag(t);
				clickView.setOnClickListener(new OnClickListener() {
					public void onClick(View arg0) {
						VipUserBean intentBean = (VipUserBean) arg0.getTag();
						if (intentBean != null) {
							Intent intent = new Intent(UserVipListAct.this,
									UserVipInfoAct.class);
							intent.putExtra("bean", intentBean);
							startActivity(intent);
						} else {
							Toast.makeText(ctx, "没有传递数据", Toast.LENGTH_SHORT)
									.show();
						}
					}
				});
			}
			// itemView.setTag(t);
		}
	}
}
